// 专业
const router = require('express').Router()
const db = require('../db')

// 得到专业列表
router.get('/list', (req, res) => {
    db.query('SELECT * FROM specialty', {}, data => {
        res.json({
            code: 0,
            data
        })
    })
})

// 新增专业
router.post('/add', (req, res) => {
    let { user } = req.query
    if (user.role !== 'admin') return;
    let values = [req.body.list]
    console.log('values', values)
    let sql = 'INSERT INTO specialty (name) VALUES ?'
    
    db.query(sql, [values], result => {
        console.log('result', result)
        res.json({
            code: 0,
            affectedRows: result.affectedRows,
            msg: '添加成功'
        })
    })
})


// 删除专业
router.post('/del', (req, res) => {
    let { user } = req.query
    if (user.role !== 'admin') return;

    let values = [req.body.list]

    let sql = 'DELETE FROM specialty where name in (?) '
    
    db.query(sql, [values], result => {
        console.log('result', result)
        res.json({
            code: 0,
            affectedRows: result.affectedRows,
            msg: '删除成功'
        })
    })
})


// 修改
router.post('/updata', (req, res) => {
    let { user } = req.query
    if (user.role !== 'admin') return;

    let values = [req.body.list]

    let sql = 'UPDATE  specialty where name in (?) '
    
    db.query(sql, [values], result => {
        console.log('result', result)
        res.json({
            code: 0,
            affectedRows: result.affectedRows,
            msg: '删除成功'
        })
    })
})
module.exports = router